﻿using Microsoft.Extensions.DependencyInjection;
using Quartz;
using SandAbp.ScheduleTask.Schedules;
using SandAbp.ScheduleTask.SchedulesSchedules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SandAbp.ScheduleTask.ScheduleMaster.Base
{
    public abstract class TaskBase
    {
        private readonly IServiceProvider _serviceProvider;
        private readonly IScheduleLogEntityRepository _scheduleLogEntityRepository;

        protected TaskBase(IServiceProvider serviceProvider, IScheduleLogEntityRepository scheduleLogEntityRepository)
        {
            _serviceProvider = serviceProvider;
            this._scheduleLogEntityRepository = scheduleLogEntityRepository;
        }



        /// <summary>
        /// 记录到日志
        /// </summary>
        /// <param name="context"></param>
        /// <param name="logModel"></param>
        protected async Task RecordTaskLog(IJobExecutionContext context, ScheduleLogEntity logModel)
        {
         
            //// 可以直接获取 JobDetail 的值
            IJobDetail job = context.JobDetail;
            var instance = job.JobDataMap["JobParam"] as ScheduleManagerDto;

            logModel.InvokeTarget = job.JobType.FullName;
            logModel.ScheduleId = instance.Id;
            logModel.JobGroup = instance.JobGroup;
            logModel.JobName = instance.Title;
            await _scheduleLogEntityRepository.InsertAsync(logModel);
        }
    }
}
